using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Triton.Common.Math
{
	[StructLayout(LayoutKind.Sequential, Pack = 1)]
	public struct BoundingBox3
	{
		public Vector3 Min;

		public Vector3 Max;

		public BoundingBox3(Vector3 min, Vector3 max)
		{
			Min = min;
			Max = max;
		}

		public bool Contains(Vector3 v)
		{
			if (v.X >= Min.X && v.X <= Max.X && v.Y >= Min.Y && v.Y <= Max.Y && v.Z >= Min.Z)
			{
				return v.Z <= Max.Z;
			}
			return false;
		}

		public ContainmentType Contains(BoundingBox3 box)
		{
			return Contains(ref this, ref box);
		}

		public static ContainmentType Contains(ref BoundingBox3 box1, ref BoundingBox3 box2)
		{
			if (!(box1.Max.X < box2.Min.X) && box1.Min.X <= box2.Max.X)
			{
				if (!(box1.Max.Y < box2.Min.Y) && box1.Min.Y <= box2.Max.Y)
				{
					if (!(box1.Max.Z < box2.Min.Z) && box1.Min.Z <= box2.Max.Z)
					{
						if (box1.Min.X <= box2.Min.X && box2.Max.X <= box1.Max.X && box1.Min.Y <= box2.Min.Y && box2.Max.Y <= box1.Max.Y && box1.Min.Z <= box2.Min.Z && box2.Max.Z <= box1.Max.Z)
						{
							return ContainmentType.Contains;
						}
						return ContainmentType.Intersects;
					}
					return ContainmentType.Disjoint;
				}
				return ContainmentType.Disjoint;
			}
			return ContainmentType.Disjoint;
		}

		public static BoundingBox3 CreateFromPoints(IEnumerable<Vector3> points)
		{
			BoundingBox3 result = new BoundingBox3(new Vector3(float.MaxValue, float.MaxValue, float.MaxValue), new Vector3(float.MinValue, float.MinValue, float.MinValue));
			bool flag = false;
			foreach (Vector3 point in points)
			{
				Vector3 v = point;
				flag = true;
				Vector3.Min(ref result.Min, ref v, out result.Min);
				Vector3.Max(ref result.Max, ref v, out result.Max);
			}
			if (!flag)
			{
				return default(BoundingBox3);
			}
			return result;
		}
	}
}
